// carPackage/pages/wallet/applyWithdraw/applyWithdraw.js
import { walletCashApply } from "../../../../service/agency_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    drawMoney: "",
    cashMoney: 0,
    showSuccessPop: false,
    userPayInfo: {},
    showBankInfoText: "",
    isDrawBtn: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cashMoney: options.money
    })
    this.setData({
      userPayInfo: app.globalData.userPayInfo
    })
    console.log("JJLog:user:" + JSON.stringify(this.data.userPayInfo));
    if (this.data.userPayInfo.bankAccount) {

      let startIndex = this.data.userPayInfo.bankAccount.length - 4;
      let endIndex = this.data.userPayInfo.bankAccount.length;
      let account = this.data.userPayInfo.bankAccount.substring(startIndex, endIndex);
      let text = "";
      if (this.data.userPayInfo.bankName) {
        text = this.data.userPayInfo.bankName + "（" + account + "）"
      } else {
        text = "银行卡" + "（" + account + "）"
      }
      this.setData({
        showBankInfoText: text
      })
    } else {
      this.setData({
        showBankInfoText: "点击完善支付信息",
        isDrawBtn: false
      })
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //输入金额:
  cellInputChange(e) {
    // console.log("JJLog:输入金额:" + JSON.stringify(e));
    this.setData({
      drawMoney: e.detail.value,
    })
  },
  //全部提现
  allDrawAction() {
    this.setData({
      drawMoney: this.data.cashMoney
    })
  },

  //确认提现:
  applyMoneyAction() {
    if (!this.data.isDrawBtn) {
      return;
    }
    if (this.data.drawMoney < 0 || !this.data.drawMoney) {
      wx.showToast({
        title: '请输入提现金额',
        icon: "none"
      })
      return;
    }
    // if (this.data.drawMoney > this.data.cashMoney) {
    //   wx.showToast({
    //     title: '提现金额已超出您的余额，请重新输入',
    //     icon: "none"
    //   }, 2500)
    //   return;
    // }
    // if (this.data.drawMoney < 10) {
    //   wx.showToast({
    //     title: '提现金额需大于10元哦!',
    //     icon: "none"
    //   })
    //   return;
    // }
    debounce(this.applyMoneyRequest);
  },

  //提现请求:
  applyMoneyRequest() {
    wx.showLoading();
    walletCashApply(this.data.drawMoney).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        //申请成功
        wx.showModal({
          title: '提示',
          content: '提现申请成功,待打款',
          showCancel: false,
          confirmColor: "#FF0000",
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 0,
              })
            }
          }
        })

      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:提现申请失败了:", err);
      wx.showToast({
        title: err || '申请失败',
        icon: "none",
        duration: 4000,
      })
    })
  },

  successPopHide() {
    this.setData({
      showSuccessPop: false
    })
  },
  sureAction() {
    this.setData({
      showSuccessPop: false
    })
  },
  //跳转到支付信息填写页面:
  pushToPayInfoPage() {
    wx.navigateTo({
      url: '/formPackage/pages/personalInfo/personalInfo',
    })
  },







})